//
//  PhotodetaliViewController.m
//  Pacific_Computer
//
//  Created by 范琦 on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PhotodetaliViewController.h"
#import "NetHandler.h"
#import "Photos_detali.h"
#import "Photos_CollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIColor+AddColor.h"
#import "Photos_TableViewCell.h"
#import "AppDelegate.h"
#import "WeiboSDK.h"
#import "WBHttpRequest.h"

@interface PhotodetaliViewController ()<UIScrollViewDelegate,WBHttpRequestDelegate>

@property (nonatomic,retain) UIScrollView *scrollView;
@property (nonatomic,retain) UIScrollView *scroll;

@property (nonatomic,retain) UIView *navigView;
@property (nonatomic,retain) UIView *indexView;
@property (nonatomic,assign) BOOL switchButton;
@property (nonatomic,retain) NSMutableArray *arr;

@end

@implementation PhotodetaliViewController

- (void)dealloc
{
    [_photo release];
    [_arr release];
    [_scrollView release];
    [_scroll release];
    [_navigView release];
    [_indexView release];
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.arr = [NSMutableArray array];
        
        self.switchButton = YES;
        
    }
    return self;
}

- (void)handler
{
    NSString *path = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v2/photo/albums?modelId=%@",self.photo.paramenterId];
    [NetHandler getDataWithUrl:path completion:^(NSData *data) {
       
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray *array = [result objectForKey:@"photos"];
        
        for (NSDictionary *dic in array) {
            Photos_detali *deta = [[Photos_detali alloc] initWithDictionary:dic];
            [self.arr addObject:deta];
            [deta release];
        }
        
        
    
        self.scroll.contentSize = CGSizeMake(self.view.bounds.size.width * self.arr.count, 0);
        for (int i = 0; i < self.arr.count; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.scroll.bounds.size.width, 0, self.scroll.bounds.size.width, self.scroll.bounds.size.height)];
            imageView.backgroundColor = [UIColor blackColor];
            
            [self.scroll addSubview:imageView];
            Photos_detali *detali = [self.arr objectAtIndex:i];
            
            NSString *path = detali.url;
            NSURL *url = [NSURL URLWithString:path];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [imageView setImageWithURL:url];
            
            
            [imageView release];
            //[detali release];
        
        }
        
        //======================================
        //右侧小图窗口
        self.scrollView.contentSize = CGSizeMake(0, (self.arr.count + 1) * (self.view.bounds.size.height / 7));
        for (NSInteger i = 0; i < self.arr.count; i++) {
            UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, i * self.scrollView.bounds.size.height / 7,self.scrollView.bounds.size.width, self.scrollView.bounds.size.height / 7)];
            scroll.tag = i + 101;
            [self.scrollView addSubview:scroll];
            [scroll release];
            
            UIImageView *viewImageOne = [[UIImageView alloc] initWithFrame:CGRectMake(scroll.bounds.size.width / 25 + 1, scroll.bounds.size.height / 20, scroll.bounds.size.width - 2 * (scroll.bounds.size.width / 25 + 1), scroll.bounds.size.height - 2 * (scroll.bounds.size.height / 20))];
            viewImageOne.alpha = 1;
            Photos_detali *detali = [self.arr objectAtIndex:i];
            
            NSString *path = detali.thumb;
            NSURL *url = [NSURL URLWithString:path];
            //viewImageOne.contentMode = UIViewContentModeScaleAspectFit;
            [viewImageOne setImageWithURL:url];
            [scroll addSubview:viewImageOne];
            [viewImageOne release];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction2:)];
            [scroll addGestureRecognizer:tap];
            [tap release];
            
            
        }
        
        //==============================================
        
        
        UILabel *LabelOne = (UILabel *)[self.view viewWithTag:1];
        NSInteger i = self.scroll.contentOffset.x / self.scroll.bounds.size.width + 1;
        NSString *numStr = [NSString stringWithFormat:@"%ld/%lu",(long)i,(unsigned long)self.arr.count];
        LabelOne.text = numStr;
        
        
        
        
        
        
        
        
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handler];
    self.navigationController.navigationBarHidden=YES;
    self.view.backgroundColor = [UIColor whiteColor];
    //self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width, 554);
//    [self.navigationController.navigationBar setBackIndicatorImage:image];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    //self.navigationController.navigationBar.translucent = NO;
    
    self.scroll.bounces = NO;
    self.scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scroll.showsVerticalScrollIndicator = NO;
    self.scroll.backgroundColor = [UIColor huiseColor];
    self.scroll.delegate = self;
    self.scroll.pagingEnabled = YES;
    [self.view addSubview:self.scroll];
    [_scroll release];
    
    //NSLog(@"kakka  = %ld",self.arr.count);
    //==========================================
    //右侧的图片预览窗口
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width / 3, self.view.bounds.size.height)];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.alpha = 0.9;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    
    
    
    
    
    
    
    
    
    
    
    //=====================
    self.navigView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 3 / 40)];
    //self.navigView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.navigView];
    [_navigView release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //button.backgroundColor = [UIColor redColor];
    button.frame = CGRectMake(0, 0, self.navigView.bounds.size.width *18 / 100, self.navigView.bounds.size.height);
    [self.navigView addSubview:button];
    
    UIImageView *buttonImage = [[UIImageView alloc] initWithFrame:CGRectMake(button.bounds.size.width / 4, button.bounds.size.height *1/3, button.bounds.size.width *2/5, button.bounds.size.height *3/5)];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImage *buttImage = [UIImage imageNamed:@"xiangleft.png"];
    buttonImage.image = buttImage;
    [button addSubview:buttonImage];
    [buttonImage release];
    
    //=====================
    self.indexView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height *7/8, self.view.bounds.size.width, self.view.bounds.size.height /8)];
    //self.indexView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.indexView];
    [_indexView release];
    
    UIView *ageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.indexView.bounds.size.width, self.indexView.bounds.size.height)];
    ageView.alpha = 0.4;
    ageView.backgroundColor = [UIColor blackColor];
    [self.indexView addSubview:ageView];
    [ageView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.indexView.bounds.size.width *4/5, self.indexView.bounds.size.height / 2)];
    titleLabel.alpha= 1;
    titleLabel.text = self.photo.name;
    titleLabel.textColor = [UIColor whiteColor];
    [self.indexView addSubview:titleLabel];
    [titleLabel release];
    
    UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.indexView.bounds.size.width *6/7, 0, self.indexView.bounds.size.width /6, self.indexView.bounds.size.height / 2)];
    numLabel.tag = 1;
    numLabel.alpha = 1;
    NSInteger i = self.scroll.contentOffset.x / self.scroll.bounds.size.width + 1;
    NSString *numStr = [NSString stringWithFormat:@"%ld/%lu",(long)i,(unsigned long)self.arr.count];
    numLabel.textColor = [UIColor whiteColor];
    numLabel.text = numStr;
    [self.indexView addSubview:numLabel];
    [numLabel release];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.scroll addGestureRecognizer:tap];
    tap.numberOfTapsRequired = 1;
    [tap release];
    
    
    
    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonTwo.tag = 3;
    buttonTwo.hidden = YES;
    //buttonTwo.backgroundColor = [UIColor yellowColor];
    buttonTwo.frame = CGRectMake(self.indexView.bounds.size.width *6/7, self.indexView.bounds.size.height *2/5, self.indexView.bounds.size.width / 8, self.indexView.bounds.size.height *3/8);
    [buttonTwo addTarget:self action:@selector(buttonClicked3:) forControlEvents:UIControlEventTouchUpInside];
    [self.indexView addSubview:buttonTwo];
    
    UIImageView *imageTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 3, buttonTwo.bounds.size.width * 2/3, buttonTwo.bounds.size.height)];
    [buttonTwo addSubview:imageTwo];
    [imageTwo release];
    UIImage *imageak = [UIImage imageNamed:@"dele.png"];
    imageTwo.image = imageak;
    
    
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonOne.tag = 2;
    //buttonOne.backgroundColor = [UIColor yellowColor];
    buttonOne.frame = CGRectMake(self.indexView.bounds.size.width *6/7, self.indexView.bounds.size.height *2/5, self.indexView.bounds.size.width / 8, self.indexView.bounds.size.height *3/8);
    [buttonOne addTarget:self action:@selector(buttonClicked2:) forControlEvents:UIControlEventTouchUpInside];
    [self.indexView addSubview:buttonOne];
    
    UIImageView *imageOne = [[UIImageView alloc] initWithFrame:CGRectMake(0, 3, buttonOne.bounds.size.width * 2/3, buttonOne.bounds.size.height)];
    [buttonOne addSubview:imageOne];
    [imageOne release];
    UIImage *imageakb = [UIImage imageNamed:@"photos.png"];
    imageOne.image = imageakb;
    
    
    
    
    UIButton *swbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    swbutton.frame = CGRectMake(self.navigView.bounds.size.width * 84 / 100, 0, self.navigView.bounds.size.width * 13.33 / 100, self.navigView.bounds.size.height);
//    [swbutton setImage:[UIImage imageNamed:@"fenxiang1"] forState:UIControlStateNormal];
    [swbutton addTarget:self action:@selector(fengxiangAction:) forControlEvents:UIControlEventTouchUpInside];
    swbutton.tintColor = [UIColor whiteColor];
    [self.navigView addSubview:swbutton];
    
    
    UIImageView *viewImage = [[UIImageView alloc] initWithFrame:CGRectMake(swbutton.bounds.size.width * 1 / 5, swbutton.bounds.size.height * 2 / 5, swbutton.bounds.size.width * 3 / 5, swbutton.bounds.size.width * 3 / 5)];
    UIImage *imageakd = [UIImage imageNamed:@"fengxiang2"];
    viewImage.image = imageakd;
    [swbutton addSubview:viewImage];
    [viewImage release];

    
    //swbutton.frame = CGRectMake(self.navigView.bounds.size.width * 86.66 / 100, self.navigView.bounds.size.height * 35 / 100, self.view.bounds.size.width * 8 / 100, self.view.bounds.size.height * 4.49 / 100);
    
    
}


- (WBMessageObject *)messageToShare
{
    
    
    WBMessageObject *message = [WBMessageObject message];
    NSString *str = [NSString stringWithFormat:@"%@ #%@#:",self.photo.url,self.photo.name];
    message.text = str;
    WBImageObject *image = [WBImageObject object];
    NSString *path = self.photo.cover;
    NSURL *url = [NSURL URLWithString:path];
    image.imageData = [NSData dataWithContentsOfURL:url];
    message.imageObject = image;
    
    
    
    
    return message;
}

- (void)fengxiangAction:(UIButton *)button
{
    NSLog(@"111111111111111");
    AppDelegate *myDelegate =(AppDelegate*)[[UIApplication sharedApplication] delegate];
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"https://api.weibo.com/oauth2/default.html";
    authRequest.scope = @"all";
    
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare] authInfo:authRequest access_token:myDelegate.wbtoken];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    //    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
    [WeiboSDK sendRequest:request];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIButton *buttonak = (UIButton *)[self.view viewWithTag:2];
    UIButton *buttonakb = (UIButton *)[self.view viewWithTag:3];
    
    UILabel *LabelOne = (UILabel *)[self.view viewWithTag:1];
    NSInteger i = self.scroll.contentOffset.x / self.scroll.bounds.size.width + 1;
    NSString *numStr = [NSString stringWithFormat:@"%ld/%lu",(long)i,(unsigned long)self.arr.count];
    LabelOne.text = numStr;
    
    NSInteger j = self.scroll.contentOffset.x / self.scroll.frame.size.width;
    self.scrollView.contentOffset = CGPointMake(0, (self.scrollView.frame.size.height / 7) * j);
    
    
    //1.UIView动画
    [UIView animateWithDuration:0.2 animations:^{
    
        if (self.scrollView.frame.origin.x == self.view.bounds.size.width *2/3) {
            
            self.scrollView.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width / 3, self.view.bounds.size.height);
            buttonakb.hidden = YES;
            buttonak.hidden = NO;
            
            if (self.switchButton == YES) {
                self.navigView.hidden = NO;
                self.indexView.hidden = NO;
                self.switchButton = NO;
            }else if (self.switchButton == NO){
                self.navigView.hidden = YES;
                self.indexView.hidden = YES;
                self.switchButton = YES;
            }
        }
            
    }];
    
    
    
    
}




- (void)tapAction:(UITapGestureRecognizer *)tap
{
  
    UIButton *buttonak = (UIButton *)[self.view viewWithTag:2];
    UIButton *buttonakb = (UIButton *)[self.view viewWithTag:3];

    
    
    //1.UIView动画
        [UIView animateWithDuration:0.2 animations:^{
    
            //有一个返回效果
            //[UIView setAnimationRepeatAutoreverses:YES];
    

    
            //动画的速度曲线
            //[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
            if (self.scrollView.frame.origin.x == self.view.bounds.size.width *2/3) {
                
                self.scrollView.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width / 3, self.view.bounds.size.height);
                buttonakb.hidden = YES;
                buttonak.hidden = NO;
                
                if (self.switchButton == YES) {
                    self.navigView.hidden = NO;
                    self.indexView.hidden = NO;
                    self.switchButton = NO;
                }else if (self.switchButton == NO){
                    self.navigView.hidden = YES;
                    self.indexView.hidden = YES;
                    self.switchButton = YES;
                }
                
            }else{
                if (self.switchButton == YES) {
                    self.navigView.hidden = NO;
                    self.indexView.hidden = NO;
                    self.switchButton = NO;
                }else if (self.switchButton == NO){
                    self.navigView.hidden = YES;
                    self.indexView.hidden = YES;
                    self.switchButton = YES;
                }
                
            }
    

            
        }];
    

    
}


- (void)tapAction2:(UITapGestureRecognizer *)tap
{
    UIImageView *image = (UIImageView *)tap.view;
    NSInteger i = image.tag - 101;
    self.scroll.contentOffset = CGPointMake(self.scroll.bounds.size.width * i, 0);
    
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    
}


- (void)buttonClicked:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
    
    
    
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"guidui" object:@"香皂" userInfo: dic];
}

- (void)buttonClicked2:(UIButton *)button
{
    
    UIButton *buttonak = (UIButton *)[self.view viewWithTag:3];
    button.hidden = YES;
    buttonak.hidden = NO;
    [self.view bringSubviewToFront:buttonak];
    
    
    
    
    //1.UIView动画
        [UIView animateWithDuration:0.2 animations:^{
    
        
            self.scrollView.frame = CGRectMake(self.view.bounds.size.width *2/3, 0, self.view.bounds.size.width  / 3, self.view.bounds.size.height);
            [self.view bringSubviewToFront:self.scrollView];
            
            [self.view bringSubviewToFront:self.indexView];
            
        }];
    
    
}


- (void)buttonClicked3:(UIButton *)button
{
    
    UIButton *buttonak = (UIButton *)[self.view viewWithTag:2];
    
    
    //1.UIView动画
    [UIView animateWithDuration:0.2 animations:^{
        
        
        button.hidden = YES;
        buttonak.hidden = NO;
        
        self.scrollView.frame = CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width / 3, self.view.bounds.size.height);
        
    }];
    
    
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
